package com.study.suanfa.shuzu;

import java.util.Arrays;

/**
 * @description: HuangXingDuiLie   环形队列
 * @date: 2020/11/28 12:56
 * @author: hcm
 * @version: 1.0
 */
public class HuangXingDuiLie {

    /**
     * 最大容量
     */
    int maxSize;
    /**
     * 头
     */
    int headPoint = 0;
    /**
     * 尾
     */
    int tailPoint = 0;
    /**
     * 底层数组
     */
    int[] arr;

    public HuangXingDuiLie(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }

    public HuangXingDuiLie() {
    }

    /**
     * 入队
     */
    public void pushQueue(int data) throws Exception {

        //队满
        if (fullFlag()) {
            throw new Exception("队列已满");
        }
        arr[tailPoint] = data;
        tailPoint = (tailPoint + 1) % maxSize;

    }

    /**
     * 出队
     */
    public int popQueue() throws Exception {
        System.out.println("移动前的头指针为" + headPoint + "尾指针为" + tailPoint);
        int res = arr[headPoint];
        headPoint = (headPoint + 1) % maxSize;
        System.out.println("移动后的头指针为" + headPoint + "尾指针为" + tailPoint);
        return res;
    }

    /**
     * 判空
     */
    public Boolean nullFlag() {
        //初始或者出队完才会想等
        System.out.println("h" + headPoint + "t" + tailPoint);
        return headPoint == tailPoint;
    }

    /**
     * 判满
     */
    public Boolean fullFlag() {
        System.out.println("res = " + (tailPoint + headPoint));
        if (tailPoint == 0) {
            return false;
        }
        return (tailPoint) % maxSize == headPoint;
    }


    public static void main(String[] args) throws Exception {
        HuangXingDuiLie h = new HuangXingDuiLie(2);
        h.pushQueue(1);
        h.pushQueue(2);
        System.out.println("pushQueue2入队之后的尾指针" + h.tailPoint);
        h.popQueue();
        h.pushQueue(3);
        h.popQueue();
        h.pushQueue(4);
        System.out.println(Arrays.toString(h.arr));
    }
}
